﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class SearchUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["/UserManagement/SearchUsers.aspxview"]!= null)
            {
                 Session["/UserManagement/ManageUsers.aspxview"] = Session["/UserManagement/SearchUsers.aspxview"];
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/AddUser.aspx");
            Session["UserAc"] = "create";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string uName = "%"+txtUserName.Text+"%";


            DataTable usrTable = new DataTable();
            DataRow newRow;

            usrTable.Columns.Add("Id");
            usrTable.Columns.Add("Name");
            usrTable.Columns.Add("Last Updated By");
            usrTable.Columns.Add("Last Updated Date");


            foreach (MembershipUser usr in Membership.FindUsersByName(uName))
            {
                Guid userGuuid = (Guid)usr.ProviderUserKey;
                string lastUpBy = "";
                string updatedDate = "";
                string name = "";

                StructureFacade StructureFacade = new StructureFacade();
                User usrObj = StructureFacade.GetUser(userGuuid);

                if (usrObj != null && usrObj.LastUpBy != Guid.Empty)
                {
                    MembershipUser adUser = Membership.GetUser(usrObj.LastUpBy);
                    User adObj = StructureFacade.GetUser(usrObj.LastUpBy);

                    lastUpBy = adObj.Name;
                    updatedDate = usrObj.LastUpdateDate.ToShortDateString();
                    name = usrObj.Name;
                }


                newRow = usrTable.NewRow();
                newRow[0] = usr.UserName;
                newRow[1] = name;
                newRow[2] = lastUpBy;
                newRow[3] = updatedDate;
                usrTable.Rows.Add(newRow);
            }

            UCGrid1.SetDataSource(usrTable);

            lblRes.Text = "Result";
            UCGridButtons1.Visible = true;
        }
    }
}
